package com.bd.rj.backstage.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bd.rj.common.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 白世俊
 * @since 2025-01-03
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    @Select("select u.*, r.rname from user u left join userrole ur ON u.id = ur.uid LEFT JOIN role r on ur.rid = r.id")
    IPage<Map<String, Object>> queryrole(IPage<Map<String, Object>> page);

    @Select("select u.*, r.rname from user u left join userrole ur ON u.id = ur.uid LEFT JOIN role r on ur.rid = r.id where r.rname = #{rname}")
    IPage<Map<String, Object>> queryrolename(IPage<Map<String, Object>> page, @Param("rname") String rname);

    @Select("select u.*, r.rname from user u left join userrole ur ON u.id = ur.uid LEFT JOIN role r on ur.rid = r.id where u.status = #{status} ")
    IPage<Map<String, Object>> querystatus(IPage<Map<String, Object>> page, @Param("status") Integer status);

    @Select("select u.*, r.rname from user u left join userrole ur ON u.id = ur.uid LEFT JOIN role r on ur.rid = r.id where u.username = #{username} ")
    IPage<Map<String, Object>> queryuname(IPage<Map<String, Object>> page, @Param("username") String username);

    Map<String,Object> getLogin(@Param("username") String username, @Param("password") String password);

    List<Map<String,Object>> getRoleUrls(@Param("rid") Long rid);

    String getUserRoles(@Param("uid") Long id);

    IPage<Map> query(@Param("page") IPage<Map> page, @Param("map") Map<String, Object> map);

    void insertUserRole(@Param("userId") Long userId);

    // 自定义方法，用于批量插入用户并关联角色（rid为2）
    void batchInsertUserRoles(@Param("uid") Long userId);

//    @Select("select u.*, r.rname from user u left join userrole ur ON u.id = ur.uid LEFT JOIN role r on ur.rid = r.id " +
//            "WHERE u.id NOT IN (SELECT ur.uid FROM userrole ur JOIN role r ON ur.rid = r.id WHERE r.id = 1)")
//    IPage<Map<String, Object>> queryroleExcludeRole1(IPage<Map<String, Object>> page);
}
